package com.oceansoft.mobile.econsole.modules.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.econsole.modules.sys.entity.ApiInfo;
import com.oceansoft.mobile.econsole.modules.sys.entity.ParamInfo;
import com.oceansoft.mobile.econsole.modules.sys.entity.Record;
import com.oceansoft.mobile.econsole.modules.sys.service.IApiInfoService;
import com.oceansoft.mobile.econsole.modules.sys.service.IParamService;
import com.oceansoft.mobile.econsole.modules.sys.service.IRecordInfoService;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.scheduled.ScheduleTasker;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.oceansoft.mobile.econsole.common.constant.Constant.DEFAULT_USER_PASSWORD;
import static org.springframework.web.bind.annotation.RequestMethod.*;

@Controller
@RequestMapping("monitortask")
public class MonitorTaskController extends BaseController {

	@Resource
	private IApiInfoService apiInfoService;
	@Resource
	private IParamService paramService;
	@Resource
	private IRecordInfoService recordInfoService;
	
	
	
	@RequestMapping(value = "startMonitor", method = GET)
	public void startMonitor(HttpServletRequest request, Model model) {
		String id = request.getParameter("id");
		ApiInfo info = apiInfoService.findApiBaseInfoById(Integer.parseInt(id));
		List<ParamInfo> params = paramService.findAllParams(info.getId());
		ScheduleTasker scheduleTasker = new ScheduleTasker(info, params,apiInfoService,paramService);
		scheduleTasker.startTask();
		
	}
	@RequestMapping(value = "updateMonitor", method = GET)
	public void updateMonitor(HttpServletRequest request, Model model) {
		String id = request.getParameter("id");
		ApiInfo info = apiInfoService.findApiBaseInfoById(Integer.parseInt(id));
		List<ParamInfo> params = paramService.findAllParams(info.getId());
		ScheduleTasker scheduleTasker = new ScheduleTasker(info, params,apiInfoService,paramService);
		scheduleTasker.reloadMonit(info.getId());
		if(info.getTimeInterval()!=0)
		{
		scheduleTasker.startTask();
		}
	
	}
	
	
	

	/**
	 * 进入监控管理首页
	 * 
	 * @return String
	 */
	@RequestMapping(value = "index", method = GET)
	public String gotoMonitorIndex() {
		return "sys/monitor-road";
	}

	/**
	 * 进入监控管理首页
	 * 
	 * @return String
	 */
	@RequestMapping(value = "vehicle", method = GET)
	public String gotoVehicleIndex(Query query, Model model) {
		query.setOrderField("sort");
		query.setOrderDirection("asc");
		Pager pager = apiInfoService.findByPage(new Query());
		model.addAttribute("query", query);
		model.addAttribute("pager", pager);
		return "sys/monitor-vehicle";
	}

	@RequestMapping(value = "vehicle", method = POST)
	public String gotoVehicleIndexByPage(Query query, Model model) {
		Pager pager = apiInfoService.findByPage(query);
		model.addAttribute("query", query);
		model.addAttribute("pager", pager);
		return "sys/monitor-vehicle";
	}

	@RequestMapping(value = "view/{api_id}", method = GET)
	public String viewVehicle(@PathVariable("api_id") int apiId, Model model) {
		if ( apiId==0) {

			return "sys/monitor-vehicle-view";
		} else {
			ApiInfo api = apiInfoService.findApiBaseInfoById(apiId);
			// int count=paramService.countParam(apiId);
			List<ParamInfo> paramlist = new ArrayList();
			paramlist = paramService.findParam(apiId);
			model.addAttribute("paramlist", paramlist);
			model.addAttribute("count", paramlist.size() - 1);
			model.addAttribute("api", api);
			return "sys/monitor-vehicle-edit";
		}
	}
	@RequestMapping(value = "DBview/{api_id}", method = GET)
	public String DBviewVehicle(@PathVariable("api_id") int apiId, Model model) {
		
			ApiInfo api = apiInfoService.findApiBaseInfoById(apiId);
			// int count=paramService.countParam(apiId);
			List<ParamInfo> paramlist = new ArrayList();
			paramlist = paramService.findParam(apiId);
			model.addAttribute("paramlist", paramlist);
			model.addAttribute("count", paramlist.size() - 1);
			model.addAttribute("api", api);
			return "sys/monitor-vehicle-dbview";
		
	}

	@ResponseBody
	@RequestMapping(value = "new", method = POST)
	public Result addApiInfo(ApiInfo apiInfo, HttpServletRequest request) {

		// List<Param> list= (List<Param>) JSONArray.toJSON(apiInfo.getParam());
		String count = request.getParameter("count");
		if ("".equals(count) || null == count) {
			count = "0";
		}
		if (null != apiInfo) {

			Result rt = apiInfoService.save(apiInfo);
			List<ParamInfo> params = new ArrayList<ParamInfo>();
			for (int i = 0; i <= Integer.parseInt(count); i++) {
				ParamInfo param = new ParamInfo();
				if(param!=null){
				param.setAllApiId(apiInfo.getId());
				param.setParamName(request.getParameter("paramName" + i));
				param.setParamValue(request.getParameter("paramValue" + i));
				param.setOrder_num(String.valueOf(i));
				paramService.saveParam(param);}
				params.add(param);
			}
			ScheduleTasker scheduleTasker = new ScheduleTasker(apiInfo, params,apiInfoService,paramService);
			scheduleTasker.startTask();
			return rt;
		}
		return new Result(Result.FAILURE, "提交参数有误");
	}

	@ResponseBody
	@RequestMapping(value = "del/{api_id}", method = DELETE)
	public Result delSysUser(@PathVariable("api_id") int apiId) {
		paramService.deleteParam(apiId);
		return apiInfoService.delete(apiId);
	}
	/**
	 * 更新
	 * @param apiInfo
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "edit", method = POST)
	public Result editApiInfo(ApiInfo apiInfo, HttpServletRequest request) {
		
		
		String count = request.getParameter("count");
		if ("".equals(count) || null == count) {
			count = "0";
		}
		paramService.deleteParam(apiInfo.getId());
		// apiInfo.setStatus(request.getParameter("status"));
		if (null != apiInfo) {
			Result rt = apiInfoService.updateApiInfo(apiInfo);
			for (int i = 0; i <= Integer.parseInt(count); i++) {
				ParamInfo param = new ParamInfo();
				if(param!=null){
					param.setAllApiId(apiInfo.getId());
					param.setParamName(request.getParameter("paramName" + i));
					param.setParamValue(request.getParameter("paramValue" + i));
					param.setOrder_num(String.valueOf(i));
					paramService.saveParam(param);}
			}
			ScheduleTasker scheduleTasker = new ScheduleTasker(apiInfo,apiInfoService,paramService);
			scheduleTasker.reloadMonit(apiInfo.getId());
			scheduleTasker.startTask();
			return rt;
		}
		return new Result(Result.FAILURE, "提交参数有误");
	}

	

	@RequestMapping(value = "record/{api_id}")
	public String RecordInfo(@PathVariable("api_id") int apiId,Query query, Model model) {
		//List<Record> list = apiInfoService.findRecordInfoById(apiId);
		query.setAPIId(apiId);
		Pager pager = apiInfoService.findRecordInfoByPage(query);
		model.addAttribute("query", query);
		model.addAttribute("pager", pager); 
		return "sys/monitor-record";
	}
	
	@RequestMapping(value = "updateStatus")
	public Result updateAPIStatus(@RequestParam("id") int id,@RequestParam("status")int status) {
		Result rt = apiInfoService.updateAPIStatus(id,status);
		return rt;	
	}
}
